/*
 *
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: UpdateDialogActivity.java
 *   @Date: 11/29/18 3:21 PM
 *   @Author: chenjun
 *   @Copyright（C）: 2018 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 *
 */

package io.bhex.baselib.update;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import io.bhex.baselib.R;

import io.bhex.baselib.mvp.BaseMVPActivity;
import io.bhex.baselib.utils.NetworkUtils;
import io.bhex.baselib.utils.ToastUtils;


public class UpdateDialogActivity extends BaseMVPActivity<UpdatePresenter, UpdatePresenter.UpdateUI>
        implements OnClickListener, OnCheckedChangeListener, UpdatePresenter.UpdateUI {

    public static final String UPDATE_DATA = "update_data";

    Button btnNow;
    Button btnLater;
    CheckBox cbIgnore;
    TextView tvUpdate;
    TextView tvUpdateSize;
    TextView tvContent;

    UpdateResponseIntf versionData;

    @Override
    protected void setContentViewExt() {
        setContentView(R.layout.act_update);

        initView();
        addEvent();
        loadData();
    }

    private void initView() {
        btnNow = viewFinder.find(R.id.update_now);
        btnLater = viewFinder.find(R.id.update_later);
        cbIgnore = viewFinder.find(R.id.update_ignored);
        tvUpdate = viewFinder.find(R.id.update_version);
        tvUpdateSize = viewFinder.find(R.id.update_version_size);
        tvContent = viewFinder.find(R.id.update_content);

    }

    public void addEvent() {
        btnNow.setOnClickListener(this);
        btnLater.setOnClickListener(this);
        cbIgnore.setOnCheckedChangeListener(this);
    }

    private void loadData() {
        versionData = (UpdateResponseIntf) getIntent().getSerializableExtra(UPDATE_DATA);

        if (versionData == null) {
            finish();
        }

        tvUpdate.setText(String.format(getResources().getString(R.string.update_new_version_code), versionData.getVersion()));

        tvUpdateSize.setText(String.format(getResources().getString(R.string.update_new_version_size), versionData.getFileSize()));

        String updateLogs = versionData.getUpdateLog();

        String[] contents = updateLogs.split("#");

        StringBuffer builder = new StringBuffer();

        for (int i = 0; i < contents.length; i++) {
            builder.append(contents[i]);
            if (i != contents.length - 1) {
                builder.append("\n");
            }
        }

        tvContent.setText(builder.toString());
    }

    @Override
    protected UpdatePresenter createPresenter() {
        return new UpdatePresenter();
    }

    @Override
    protected UpdatePresenter.UpdateUI getUI() {
        return this;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            //如果这个被勾选上，用户选择“忽略此版本”
            UpdateAgent.ignored(this, versionData);
            finish();
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.update_now) {
            if (!NetworkUtils.isWifiConnected(this)) {
                ToastUtils.showLong(this,getResources().getString(R.string.update_use_wifi));
            }
            UpdateAgent.downLoad(this, versionData);

        }

        finish();
    }

    @Override
    public void showProgressDialog() {

    }

    @Override
    public void showProgressDialog(String title, String hint) {

    }

    @Override
    public void dismissProgressDialog() {

    }
}
